<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-if="false" v-model="listQuery.lineId" :placeholder="$t('equipment.lineNumber')" class="filter-item" style="width: 200px">
        <el-option v-for="item in lineOptions" :key="item.lineId" :label="item.lineName" :value="item.lineId" />
      </el-select>
      <el-select v-model="listQuery.stationNumber" :placeholder="$t('equipment.stationNumber')" class="filter-item" clearable style="width: 200px">
        <el-option v-for="station in stationOptions" :key="station.stationNumber" :label="station.stationName+'_'+station.stationNumber" :value="station.stationNumber" />
      </el-select>
      <el-date-picker v-model="listQuery.startTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd HH:mm:ss" :placeholder="$t('common.startDate')" class="filter-item" />
      <el-date-picker v-model="listQuery.endTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd HH:mm:ss" :placeholder="$t('common.endDate')" class="filter-item" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('common.search') }}
      </el-button>
    </div>
    <!-- 主界面数据 -->
    <el-table
      ref="dragTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      :header-row-style="{color: 'black'}"
      :header-cell-style="{background: 'lightBlue'}"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" width="80">
        <template slot-scope="{}">
          <svg-icon class="drag-handler" icon-class="drag" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.lineNumber')" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.lineNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.stationNumber')" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.stationNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.stationName')" width="auto" align="center">
        <template slot-scope="{row}">
          <span>{{ row.stationName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('rework.okQuantity')" width="150px" align="center">
        <template slot-scope="{row}">
          <span style="color:green">{{ row.okQuantity }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('rework.ngQuantity')" width="150px" align="center">
        <template slot-scope="{row}">
          <span style="color:red">{{ row.ngQuantity }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('rework.okRate')+ '(%)'" width="150px" align="center">
        <template slot-scope="{row}">
          <span style="color:blue">{{ row.rate }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-row style="background:#fff;margin-bottom:32px;margin-top: 10px;">
      <line-chart :chart-data="lineChartData" item-color="red" />
    </el-row>
    <el-row style="background:#fff;margin-bottom:32px;margin-top: 10px;">
      <line-chart :chart-data="lineChartData2" item-color="green" />
    </el-row>
  </div>
</template>

<script>
import { getLineList, getStationListEx } from '@/api/equipment'
import { getAbnormalPartCountOfStation } from '@/api/rework'
import waves from '@/directive/waves' // waves directive
import LineChart from '@/components/Charts/LineChart'
import { parseTime } from '@/utils'
import i18n from '@/lang'

export default {
  name: 'FailSummary',
  components: {
    LineChart
  },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      editRight: true,
      stationOptions: [],
      tableKey: 0,
      list: [],
      newList: [],
      sortable: null,
      lineOptions: [],
      lineChartData: {
        valueY: [],
        valueX: [],
        title: i18n.t('rework.ngQuantity')
      },
      lineChartData2: {
        valueY: [],
        valueX: [],
        title: i18n.t('rework.okRate')
      },
      // 任务u状态
      stateOptions: [{ label: '所有', key: 0, color: 'white' },
        { label: '进行中', key: 1, color: 'green' },
        { label: '延期', key: 2, color: 'orange' },
        { label: '完成', key: 3, color: 'darkblue' },
        { label: '取消', key: 4, color: 'red' },
        { label: '暂停', key: 5, color: 'yellow' }],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        state: 0,
        lineId: this.$store.getters.lineId,
        lineName: '',
        lineNumber: '',
        stationNumber: '',
        startTime: parseTime(Date.now(), '{y}-{m}-{d} 00:00:00'),
        endTime: '',
        sort: '+sequence'
      },
      showReviewer: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: i18n.t('common.edit'),
        create: i18n.t('common.creatNew')
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        stationNumber: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        stationName: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        lineNumber: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        processNumber1: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        processName1: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }]
      },
      downloadLoading: false,
      searchLoading: {
        director: false
      },
      searchDialogStatus: '',
      searchDialogFormVisible: {
        director: false
      }
    }
  },
  created() {
    this.editRight = (this.$store.getters.roles === 'admin' || this.$store.getters.roles === 'administrator')
    this.handleLineSelect()
    this.getLineList()
    this.getList()
  },
  methods: {
    timestepToolTip(index) {
      return index + '%'
    },
    formatDateTime(date) {
      return date.substring(0, 10)
    },
    isEmptyLineId: function() {
      var ret = (this.listQuery.lineId === undefined || this.listQuery.lineId === '')
      if (ret) {
        this.$notify({
          title: this.$i18n.t('common.tip'),
          message: i18n.t('msg.NO_LINE_SELECTED'),
          type: 'success',
          duration: 2000
        })
      }
      return ret
    },
    // 获取线体清单
    getLineList() {
      this.listLoading = true
      getLineList(this.listQuery).then(response => {
        this.lineOptions = response.data.lines
        if (this.lineOptions.length > 0) {
          this.listQuery.lineId = this.lineOptions[0].lineId
        }
        this.listLoading = false
      })
    },
    handleLineSelect() {
      this.stationOptions = []
      getStationListEx(this.listQuery).then(response => {
        this.stationOptions = response.data.stations
      })
    },
    // 获取清单
    getList() {
      if (this.isEmptyLineId() === false) {
        this.listLoading = true
        getAbnormalPartCountOfStation(this.listQuery).then(response => {
          this.list = response.data.stations
          this.total = response.data.total
          this.lineChartData.valueY = []
          this.lineChartData.valueX = []
          this.lineChartData2.valueY = []
          this.lineChartData2.valueX = []
          this.list.forEach(e => {
            this.lineChartData.valueY.push(e.ngQuantity)
            this.lineChartData.valueX.push(e.stationName)
            this.lineChartData2.valueY.push(e.rate)
            this.lineChartData2.valueX.push(e.stationName)
          })
          this.listLoading = false
        })
      }
    },
    // 查看工艺参数
    handleCheckProcess(lineId, processNumber) {
      this.$router.push({ path: '/equipment/processResultDef', query: { lineId: lineId, processNumber: processNumber }})
    },
    // 点击搜索时获取清单
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 过滤相关信息
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  height: 800px;
  background-color: white;
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px x0;
    //margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
